﻿using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Mappers.Resolvers;
using Enferno.Public.Web.Models;

namespace Enferno.Public.Web.Mappers.BasketProfiles
{
    public class BasketToBasketModelProfile :
        BasketToBasketBaseModelProfile
            <BasketModel, BasketItemModel, PromotionModel, OnHandStatusModel, PriceModel, ProductFileModel>
    {
        
    }

    public class BasketToBasketBaseModelProfile<TBasketModel, TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel> : Profile
        where TBasketModel : BasketBaseModel<TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel>
        where TBasketItemModel : BasketItemBaseModel<TPriceModel>
        where TPromotionModel : PromotionBaseModel<TProductFileModel>
        where TOnHandStatusModel : OnHandStatusModel, new()
        where TPriceModel : PriceModel
        where TProductFileModel : ProductFileModel
    {
        protected override void Configure()
        {
            var siteRules = IoC.Resolve<ISiteRules>();
            Mapper.CreateMap<StormApiClient.Shopping.Basket, TBasketModel>()
                .ForMember(basketModel => basketModel.Promotions,
                    basketConfig => basketConfig.ResolveUsing<PromotionModelsResolver<TPromotionModel, TProductFileModel>>())
                .ForMember(basketModel => basketModel.OnHand,
                    basketConfig => basketConfig.MapFrom(basket => siteRules.GetOnHandStatus<TOnHandStatusModel>(basket)))
                .ForMember(to => to.Freights,
                    opts => opts.ResolveUsing<BasketModelFreightsResolver<TBasketItemModel, TPriceModel>>())
                .ForMember(to => to.Payments,
                    opts => opts.ResolveUsing<BasketModelPaymentsResolver<TBasketItemModel, TPriceModel>>())
                .ForMember(to => to.Items,
                    opts => opts.ResolveUsing<BasketModelItemsResolver<TBasketItemModel, TPriceModel>>());
        }

        public override string ProfileName
        {
            get { return GetType().Name; }
        }
    }
}
